package com.hdaccp.user.controller;




import com.hdaccp.pojo.po.Shop;
import com.hdaccp.pojo.po.User;
import com.hdaccp.user.dao.UserDao;
import com.hdaccp.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/User")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired 
    private UserDao userDao;
    //查询所有用户
    @RequestMapping("/query")
    public List<User> query(){
        return userDao.findAll();

    }
    
    //根据用户id查询
    @RequestMapping("/getOne/{Id}")
    public User getOne(@PathVariable("Id") int Id)
    {
        return userDao.findById(Id).get();
    }
    //用户分页查询
    @RequestMapping("/queryPage/{cpage}/{pagesize}")
    public List<User> queryPage(@PathVariable("cpage") int cpage,@PathVariable("pagesize") int pagesize){
        return userService.queryPage(cpage,pagesize );
    }


    @RequestMapping("/save")
    public String save( String userId, String userpwd, String username,int state){
        String message="1";
        User user=new User();
        user.setUserId(userId);
        user.setUserpwd(userpwd);
        user.setUsername(username);
        user.setState(state);
        try { 
            userDao.save(user);
        } catch (Exception e){
            message="2";
        }

        
        
        return message;
    }
    
    
    
    //登陆 验证账号密码是否正确
    
    @RequestMapping("/login")
    public String login(String userId, String userpwd){
        String msg="1";

        try {
            User user=userService.login(userId,userpwd);

        } catch (Exception e) {
            msg="2";
        }
        return msg;


        
        
        
    }
    @RequestMapping("/del/{Id}")
    public String del(@PathVariable int Id){
        String message="111";

        try {

            userDao.deleteById(Id);
        } catch (Exception e){
            message="222";
        }


        return message;
    }
    /**
     * 修改状态
     * @param id
     * @return
     */
    @RequestMapping("/updatestate/{Id}/{state}")
    public String updateState(@PathVariable("Id") int Id,@PathVariable("state") int state){
        String msg = "success";
        //String createTime = new SimpleDateFormat("yyyy-MM-dd").format(new Date());获取当前时间字符串
        User user = userDao.findById(Id).get();
        user.setState(state);
        try {
            userDao.save(user);
        } catch (Exception e) {
            msg = "失败";
        }
        return msg;
    }


}
